package com.oig.sys.msg.controller;


import com.oig.common.module.SysResponse;
import com.oig.common.module.ValidListDto;
import com.oig.common.validator.GroupChange;
import com.oig.common.validator.GroupComplete;
import com.oig.sys.msg.app.IAppExportTaskService;
import com.oig.sys.msg.dto.TaskDto;
import com.oig.sys.msg.service.ITbExportTaskService;
import com.oig.sys.msg.service.ITbNoticeMsgService;
import com.oig.sys.msg.service.ITbTaskService;
import com.oig.sys.msg.vo.DashboardDataVo;
import com.oig.sys.msg.vo.MsgNumTipVo;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Tag(name = "用户消息及下载任务处理")
@RestController
@RequestMapping(value = "/userMsgTask")
@Slf4j
@RequiredArgsConstructor
public class UserMsgTaskController {

    private final IAppExportTaskService appExportTaskService;
    private final ITbExportTaskService exportTaskService ;
    private final ITbNoticeMsgService noticeMsgService ;
    private final ITbTaskService taskService;


    @Operation(summary = "获取用户任务消息提醒汇总数量")
    @PostMapping(value = "/getSumInfo/{userId}")
    public MsgNumTipVo getSumInfo(@PathVariable("userId") Integer userId){
        log.info(" getSumInfo:{}", userId);
        MsgNumTipVo tipVo = new MsgNumTipVo();
        tipVo.setUnDownTaskNum(this.exportTaskService.findUnDownSizeByUserId(userId));
        tipVo.setUnReadMsgNum(this.noticeMsgService.updatePushedUnReadMsgNum(userId));
        tipVo.setUnDoTaskNum(this.taskService.findUnDoSizeByUserId(userId));
        return tipVo;
    }

    @Operation(summary = "获取用户首页面板统计数据")
    @PostMapping(value = "/getDashboardData/{userId}")
    public DashboardDataVo getDashboardData(@PathVariable("userId") Integer userId){
        DashboardDataVo dashboardDataVo = new DashboardDataVo();
        dashboardDataVo.setUnDoTaskNum(this.taskService.findUnDoSizeByUserId(userId));
        dashboardDataVo.setFinishTaskNum(this.taskService.findFinishTaskSizeByUserId(userId));
        return dashboardDataVo;
    }

    @Operation(summary = "导出任务标记为删除,允许批量操作，标记为删除后不展示")
    @PostMapping(value = "/calExportTask")
    public SysResponse<List<Integer>> calExportTask(@RequestBody @Validated ValidListDto<Integer> taskIdListDto){
        log.info(" updateTaskDel:{}--{}", SecurityUtil.getDefineUserMain().getRealName(), taskIdListDto);
        this.exportTaskService.calExportTask(taskIdListDto.getList(),SecurityUtil.getDefineUserMain().getId());
        return SysResponse.ok(taskIdListDto.getList(),"删除成功");
    }

    @Operation(summary = "下载,只对异步导出任务生成的文件下载")
    @PostMapping(value = "/download/{exportTaskId}")
    public ResponseEntity<byte[]> download(@PathVariable(value = "exportTaskId") Integer exportTaskId) {
        log.info(" download:{}--{}", SecurityUtil.getDefineUserMain().getRealName(), exportTaskId);
        return this.appExportTaskService.download(exportTaskId);
    }


    /**
     * 消息公告标记已读
     */
    @Operation(summary = "消息公告标记已读,允许批量操作")
    @PostMapping(value = "/updateMsgRead")
    public SysResponse<List<Integer>> updateMsgRead(@RequestBody @Validated ValidListDto<Integer> msgIdListDto){
        log.info(" updateRead:{}--{}", SecurityUtil.getDefineUserMain().getRealName(), msgIdListDto);
        this.noticeMsgService.updateRead(msgIdListDto.getList(),SecurityUtil.getDefineUserMain().getId());
        return SysResponse.ok(msgIdListDto.getList());
    }

    /**
     * 消息公告标记删除
     */
    @Operation(summary = "消息公告标记删除,允许批量操作")
    @PostMapping(value = "/updateMsgDel")
    public SysResponse<List<Integer>> updateMsgDel(@RequestBody @Validated ValidListDto<Integer> msgIdListDto){
        log.info(" updateRead:{}--{}", SecurityUtil.getDefineUserMain().getRealName(), msgIdListDto);
        this.noticeMsgService.updateDel(msgIdListDto.getList(),SecurityUtil.getDefineUserMain().getId());
        return SysResponse.ok(msgIdListDto.getList());
    }


    /**
     * 消息公告全部设置已读
     */
    @Operation(summary = "消息公告全部设置已读")
    @PostMapping(value = "/updateMsgReadAll")
    public SysResponse<String> updateMsgReadAll(){
        log.info(" updateReadAll:{}", SecurityUtil.getDefineUserMain().getRealName());
        this.noticeMsgService.updateReadAll(SecurityUtil.getDefineUserMain().getId());
        return SysResponse.ok("已全部标记已读");
    }

    /**
     * 消息公告全部删除
     */
    @Operation(summary = "消息公告全部删除")
    @PostMapping(value = "/updateMsgDelAll")
    public SysResponse<String> updateMsgDelAll(){
        log.info(" updateDelAll:{}", SecurityUtil.getDefineUserMain().getRealName());
        this.noticeMsgService.updateDelAll(SecurityUtil.getDefineUserMain().getId());
        return SysResponse.ok("已全部标记已读");
    }


    /**
     * 开始任务
     */
    @Operation(summary = "开始任务")
    @PostMapping(value = "/updateTaskStart")
    public SysResponse<String> startTask(@RequestBody @Validated(GroupComplete.class) TaskDto taskDto){
        log.info("{}, start task: {}", SecurityUtil.getDefineUserMain().getRealName(), taskDto) ;
        taskDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        taskDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        taskDto.setCreateTime(new Date());
        this.taskService.startTask(taskDto);
        return SysResponse.ok("开始任务");
    }

    /**
     * 拒绝任务
     */
    @Operation(summary = "拒绝任务")
    @PostMapping(value = "/updateTaskRefuse")
    public SysResponse<String> refuseTask(@RequestBody @Validated(GroupComplete.class) TaskDto taskDto){
        log.info("{}, refuse task: {}", SecurityUtil.getDefineUserMain().getRealName(), taskDto) ;
        taskDto.setLastUpdateId(SecurityUtil.getUserForIfNullBuild().getId());
        taskDto.setLastUpdateName(SecurityUtil.getUserForIfNullBuild().getRealName());
        taskDto.setLastUpdateTime(new Date());
        this.taskService.refuseTask(taskDto);
        return SysResponse.ok("拒绝任务");
    }

    /**
     * 完成自己的任务
     */
    @Operation(summary = "任务完成")
    @PostMapping(value = "/updateTaskFinish")
    public SysResponse<String> finishTask(@RequestBody @Validated(GroupComplete.class) TaskDto taskDto){
        log.info("{}, finish task: {}", SecurityUtil.getDefineUserMain().getRealName(), taskDto) ;
        taskDto.setLastUpdateId(SecurityUtil.getUserForIfNullBuild().getId());
        taskDto.setLastUpdateName(SecurityUtil.getUserForIfNullBuild().getRealName());
        taskDto.setLastUpdateTime(new Date());
        this.taskService.finishTask(taskDto);
        return SysResponse.ok("任务完成");
    }


    /**
     * 任务转给他人
     */
    @Operation(summary = "指派给他人")
    @PostMapping(value = "/updateTaskAssign")
    public SysResponse<String> assignTask(@RequestBody @Validated(GroupChange.class) TaskDto taskDto){
        log.info("{}, assign task: {}", SecurityUtil.getDefineUserMain().getRealName(), taskDto) ;
        taskDto.setLastUpdateId(SecurityUtil.getUserForIfNullBuild().getId());
        taskDto.setLastUpdateName(SecurityUtil.getUserForIfNullBuild().getRealName());
        taskDto.setLastUpdateTime(new Date());
        this.taskService.assignTask(taskDto);
        return SysResponse.ok("任务指派成功");
    }




}
